package com.zly.entity.enums;

import java.util.*;

import javafx.util.Pair;

public enum BlockType {

    WHEN_BLOCK("when_block"),
    OTHERWISE_BLOCK("otherwise_block"),
    EXPLICIT_NORMAL_BLOCK("normal_block"),
    IMPLICIT_NORMAL_BLOCK(null),
    ;

    //用于维护从BlockType到Keyword的双向映射
    private static final List<Pair<BlockType,Keyword>> BK_LIST = new ArrayList<>();
    static {
        BK_LIST.add(new Pair<>(WHEN_BLOCK,Keyword.BLOCK_TYPE_ABBR_WHEN));
        BK_LIST.add(new Pair<>(OTHERWISE_BLOCK,Keyword.BLOCK_TYPE_ABBR_OTHERWISE));
        BK_LIST.add(new Pair<>(EXPLICIT_NORMAL_BLOCK,Keyword.BLOCK_TYPE_ABBR_NORMAL));
    }

    private final String strValue;

    private BlockType(String strValue) {
        this.strValue = strValue;
    }

    //从字符串值转为枚举值
    public static BlockType from(String strValue) {
        for(BlockType blockType : values()) {
            if(blockType.strValue.equals(strValue)) {
                return blockType;
            }
        }
        return null;
    }

    //从Keyword枚举值转为该枚举的枚举值
    public static BlockType from(Keyword keyword) {
        for(Pair<BlockType,Keyword> pair : BK_LIST) {
            if(pair.getValue() == keyword) {
                return pair.getKey();
            }
        }
        return null;
    }

    //获取所有有对应缩写Keyword的此枚举类的枚举
    public static BlockType[] hasAbbrKeywordValues(){
        BlockType[] result = new BlockType[BK_LIST.size()];
        for(int i = 0; i < BK_LIST.size(); i++){
            result[i] = BK_LIST.get(i).getKey();
        }
        return result;
    }

    //获取该BlockType枚举对应的Keyword枚举
    public Keyword toKeyword() {
        for(Pair<BlockType,Keyword> pair : BK_LIST) {
            if(this == pair.getKey()) {
                return pair.getValue();
            }
        }
        return null;
    }

    public String getStrValue() {
        return strValue;
    }

}
